cap log close
snapshot erase _all
clear all
estimates drop _all
collect clear

*===========================*
*	Set Globals				*
*===========================*

	global filenum 12
	
	global nonTW_samp 		1		// set to 0 or 1 to run on non-tw sample	
	
*===========================*
*	Set Locals				*
*===========================*
	
	#delimit ;
	
		*** SETUP VARIABLES EDIT VERY LIGHTLY;
		local m_list 		"12 1 3 4 5 6";
		
		local keep_list 	"m12_x_small_y2020 m1_x_small_y2020 
							 m3_x_small_y2020 m4_x_small_y2020 m5_x_small_y2020 
							 m6_x_small_y2020";
						 
		local fixed_effect	"datevar#statefip";		// can add ind_2digit where appropriate
		
		local outcomes 		"absent_paid";
		
		*** SUBSAMPLE VARIABLES ;
		
			** OUTER LOOP VARIABLE ;		
				local sample_var1	"cdc_high_m4"; // some_sick_leave
						
				local samp1			"cdc_high";

			** INNER LOOP VARIABLE ;
				local sample_var2	gender_parent;			// options: some_sick_leave, female, covid_low_m4 covid_detail childUnder12 

				local samp2			"gender_parent";					// options: PSL, fem, apr_low covid_detail childUnder12
				
			
	#delimit cr
	
*===========================*
*	Set File Name			*
*===========================*

		if ${nonTW_samp}==1		local file_tag 	"`samp1'_`samp2'_TW0"
		if ${nonTW_samp}==0		local file_tag 	"samp1'_`samp2'"
		
*===========================*
*	Load In Analysis File	*
*===========================*
	log using "${log_path}/${filenum}_`file_tag'.smcl", replace
	
	use "${data_path}/analysis_file.dta" , clear
	
	if ${nonTW_samp} == 1		keep if tw_binary_10ly==0	
			
	** Generate the CDC subsample
		gen cdc_high_m4 = 1 			if (cdc_levels_m4==4)
			replace cdc_high_m4 = 0 	if (cdc_levels_m4==1) 

	** Testing an income cut		
		sum inc_asec [w=wtfinl],d
		gen income_subsample = 1 	 if inc_asec<=r(p25)
		replace income_subsample = 2 if inc_asec>=r(p75)
			
	
	** Pickup Levels for Loops
		levelsof `sample_var1', local(samp_levels1)
		levelsof `sample_var2', local(samp_levels2)		
	
snapshot save
	
*===================*
* 	DD in Time		*
* 	18/19 vs 20		*
*===================*

local counter = 0
foreach v in `outcomes'{
	
	foreach x1 in `samp_levels1'{
		
		foreach x2 in `samp_levels2'{
			
			local counter = `counter'+1

			if `counter' == 1		local r "replace"
			if `counter' != 1		local r "append"
			
			
			snapshot restore 1
			
			keep if `sample_var1'==`x1' & `sample_var2'==`x2' 	// select subsample
						
			#delimit ;
			
				** NO CONTROLS **;
				reghdfe `v' 
						m12-m6 y2020 small
						small_x_y2020
						m12_x_y2020-m6_x_y2020
						m12_x_small-m6_x_small
						m12_x_small_y2020 - m6_x_small_y2020
						[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
									
				test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
					scalar pval=`r(p)';					

				qui sum `v' if asec_year!=2020 & small==1 [w=wtfinl];
					scalar control_mean = `r(mean)';

				est sto ddd1_`x1'`x2';
				estadd local 	controls 	"NO";
				estadd local 	nonTW		"${nonTW_samp}";
				estadd local 	s1 			"`samp1' `x1'";
				estadd local 	s2			"`samp2' `x2'";
				estadd scalar 	cmean 		control_mean;
				estadd scalar 	parallel 	pval;	
				
				regsave `keep_list'
					using "${out_path}/${filenum}_`v'_`file_tag'.dta",
					addlabel(outcome, "`v'", controls, "NO", s1, "`samp1' `x1'", s2, "`samp2' `x2'", mu, `=control_mean') `r';
				
				** WITH CONTROLS **;
				
				reghdfe `v' 
						m12-m6 y2020 small
						small_x_y2020
						m12_x_y2020-m6_x_y2020
						m12_x_small-m6_x_small
						m12_x_small_y2020 - m6_x_small_y2020
						bachelors age age2 childUnder12
						[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
							
				test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
					scalar pval=`r(p)';					
								
				est sto ddd2_`x1'`x2';
				estadd local 	controls 	"YES";
				estadd local 	nonTW		"${nonTW_samp}";
				estadd local 	s1 			"`samp1' `x1'";
				estadd local 	s2			"`samp2' `x2'";
				estadd scalar 	cmean 		control_mean;
				estadd scalar 	parallel 	pval;	
				
			regsave `keep_list'
				using "${out_path}/${filenum}_`v'_`file_tag'.dta",
				addlabel(outcome, "`v'", controls, "YES", s1, "`samp1' `x1'", s2, "`samp2' `x2'", mu, `=control_mean') append;
				
			#delimit cr
			
		} // end inner loop
	} // end outer loop
	
	#delimit ;
		esttab *
			using "${out_path}/${filenum}_`v'_`file_tag'.csv",
			replace	label se star(* 0.1 ** .05 *** 0.01)
			s(cmean nonTW controls s1 s2 parallel N, 
				label("Control Mean" "Non TW Sample" "Controls" "Subsample1" "Subsample 2"  "Parallel Trends" "Observations"))
			keep(`keep_list');	
	
	#delimit cr
	
} // end outcomes loop
log close
